package Admin;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import General.AdminServiceInterface;
import General.Product;
import General.ShipmentInterface;
import General.ShipmentIteratorInterface;

public class Admin {
	
	private String fHost;
	private String fRegistryIp;
	private int fRegistryPort;

	public Admin(String host, String registryIp, int registryPort)  {
		fHost = host;
		fRegistryIp = registryIp;
		fRegistryPort = registryPort;
	}
	
	public void start() {
		try {
			
			// Look up registry
			Registry registry = LocateRegistry.getRegistry(fRegistryIp, fRegistryPort);
			
			// Look up AdminService
			AdminServiceInterface admin = (AdminServiceInterface)registry.lookup("AdminService");
			
			// Output
			System.out.println("[ADMIN] Admin started on host " + fHost);
			
			// Change base rate of AIR shipment type
			System.out.println("[ADMIN] Change base rate for air shipment type");
			admin.changeRate(Product.AIR, 40);
			
			// List all pending shipments
			ShipmentIteratorInterface it = admin.getPendingShipments();
			System.out.println("[ADMIN] List all pending shipments.");
			while (it.hasNext()) {
				ShipmentInterface shipment = it.next();
				if (shipment != null) {
					System.out.println("[ADMIN]    " + shipment.getProduct().getName());
				} else {
					System.out.println("ERROR");
				}
			}
			
			// List all finished shipments
			ShipmentIteratorInterface it2 = admin.getHistoryShipments();
			System.out.println("[ADMIN] List all finished shipments.");
			while (it2.hasNext()) {
				ShipmentInterface shipment = it2.next();
				if (shipment != null) {
					System.out.println("[ADMIN]    " + shipment.getProduct().getName());
				} else {
					System.out.println("ERROR");
				}
			}
	
		} catch (AccessException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}
